package com.haiqi.modules.map.web;

import com.github.pagehelper.PageInfo;
import com.haiqi.common.BaseController;
import com.haiqi.common.annotation.SafeAuthority;
import com.haiqi.common.bean.DataGridBean;
import com.haiqi.common.bean.PageBean;
import com.haiqi.common.persistence.entity.AisShip;
import com.haiqi.common.persistence.entity.CompanyMapSearch;
import com.haiqi.common.persistence.entity.MobileShip;
import com.haiqi.common.utils.HttpRequestUtils;
import com.haiqi.common.utils.PageUtils;
import com.haiqi.modules.main.bean.ResponseBean;
import com.haiqi.modules.map.service.CompanyMapSearchService;
import com.haiqi.modules.map.service.MobileShipService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * Created by hythzx on 2016/6/23.
 */
@Controller
@RequestMapping("/manage/map/company")
@SafeAuthority(menuCode = "companyMonitorLocation")
public class CompanyMapController extends BaseController{


    @Autowired
    private MobileShipService cyxShipService;


    @Autowired
    private CompanyMapSearchService companyMapSearchService;

    @Value("${geo.server}")
    private String geoServer;

    @RequestMapping("/index")
    public String index(HttpSession session, Model model){
        ResponseBean.UserBean user = getUser(session);
        model.addAttribute("company",user.getOfficeid());
        return "/modules/map/company_map";
    }

    @RequestMapping("/aisShip")
    public String aisShip(HttpSession session, Model model){
        ResponseBean.UserBean user = getUser(session);
        model.addAttribute("company",user.getOfficeid());
        return "/modules/map/window/aisShip";
    }

    @RequestMapping("/cyxShip")
    public String cyxShip(HttpSession session, Model model){
        ResponseBean.UserBean user = getUser(session);
        model.addAttribute("company",user.getOfficeid());
        return "/modules/map/window/cyxShip";
    }

    @RequestMapping("/cyxShip/ajax/list")
    @ResponseBody
    public DataGridBean<MobileShip> cyxShip(HttpSession session,MobileShip cyxShip, PageBean page){
        PageUtils.generatePage(page);
        ResponseBean.UserBean user = getUser(session);
        cyxShip.setCompanyId(user.getOfficeid());
        List<MobileShip> dataList = cyxShipService.getDataList(cyxShip);
        PageInfo<MobileShip> pageInfo = new PageInfo<>(dataList);
        DataGridBean<MobileShip> mobileShipDataGridBean = new DataGridBean<>();
        PageUtils.convertToEasyuiList(pageInfo, mobileShipDataGridBean);
        return mobileShipDataGridBean;
    }


    @RequestMapping("/search")
    @ResponseBody
    public PageInfo<CompanyMapSearch> search(HttpSession session,CompanyMapSearch companyMapSearch, PageBean page, String key){
        page.setLimit(10);
        PageUtils.generatePage(page);
        ResponseBean.UserBean user = getUser(session);
        companyMapSearch.setCompany(user.getOfficeid());
        companyMapSearch.setKeyword(key);
        companyMapSearch.setName(key);
        companyMapSearch.setMmsi(key);
        companyMapSearch.setQuayJc(key);
        companyMapSearch.setQuayNum(key);
        List<CompanyMapSearch> dataList = companyMapSearchService.getDataList(companyMapSearch);
        PageInfo<CompanyMapSearch> pageInfo = new PageInfo<>(dataList);
        return pageInfo;
    }

    @RequestMapping("/aisShip/ajax/list")
    @ResponseBody
    public DataGridBean<AisShip> aisShipList(AisShip aisShip, PageBean page,String key){
        PageUtils.generatePage(page);
        aisShip.setShipname(key);
        aisShip.setMmsi(key);
        List<AisShip> dataList = cyxShipService.aisShipList(aisShip);
        PageInfo<AisShip> pageInfo = new PageInfo<>(dataList);
        DataGridBean<AisShip> mobileShipDataGridBean = new DataGridBean<>();
        PageUtils.convertToEasyuiList(pageInfo, mobileShipDataGridBean);
        return mobileShipDataGridBean;
    }

    /**
     * 航次信息报告
     * @param session
     * @param shipid
     * @param model
     * @return
     */
    @RequestMapping("/voyage")
    public String voyage(HttpSession session,String shipid, Model model){
        ResponseBean.UserBean user = getUser(session);
        model.addAttribute("company",user.getOfficeid());
        if(StringUtils.isNotEmpty(shipid)){
            model.addAttribute("shipid",shipid);
        }
        return "/modules/map/window/voyage";
    }

    @RequestMapping("/accident")
    public String accident(HttpSession session,String shipid, Model model){
        ResponseBean.UserBean user = getUser(session);
        model.addAttribute("company",user.getOfficeid());
        if(StringUtils.isNotEmpty(shipid)){
            model.addAttribute("shipid",shipid);
        }
        return "/modules/map/window/accident";
    }

    @RequestMapping("/message")
    public String message(HttpSession session, Model model,String ships){
        ResponseBean.UserBean user = getUser(session);
        model.addAttribute("company",user.getOfficeid());
        if(StringUtils.isNotEmpty(ships)){
            model.addAttribute("ships",ships);
        }
        return "/modules/map/window/message";
    }

    @ResponseBody
    @RequestMapping("/gf")
    public String gf(Map map, HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        String s = HttpRequestUtils.sendPost(geoServer, parameterMap);
        return s;
    }

    @RequestMapping("/touchLine")
    public String touchLine(Model model,String shipid,String iemi){
        if(StringUtils.isNotEmpty(shipid)){
            model.addAttribute("shipid",shipid);
        }
        if(StringUtils.isNotEmpty(iemi)){
            model.addAttribute("iemi",iemi);
        }
        return "/modules/map/window/touchLine";
    }

    /**
     *
     */
    @RequestMapping("/fleet")
    public String fleet(HttpSession session, Model model){
        ResponseBean.UserBean user = getUser(session);
        model.addAttribute("company",user.getOfficeid());
        return "/modules/map/window/myfleet";
    }
    @RequestMapping("/fleetManager")
    public String fleetManager(HttpSession session, Model model){
        ResponseBean.UserBean user = getUser(session);
        model.addAttribute("company",user.getOfficeid());
        return "/modules/map/window/fleetManager";
    }
    @RequestMapping("/myAttention1")
    public String myAttention1(HttpSession session, Model model){
        ResponseBean.UserBean user = getUser(session);
        model.addAttribute("company",user.getOfficeid());
        session.setAttribute("sfid","1");
        return "/modules/map/window/myAttention1";
    }
    @RequestMapping("/myAttention2")
    public String myAttention2(HttpSession session, Model model){
        ResponseBean.UserBean user = getUser(session);
        model.addAttribute("company",user.getOfficeid());
        session.setAttribute("sfid","2");
        return "/modules/map/window/myAttention1";
    }
    /**
     *
     */
    @RequestMapping("/control")
    public String control(HttpSession session, Model model,String shipid){
        ResponseBean.UserBean user = getUser(session);
        model.addAttribute("company",user.getOfficeid());
        if(StringUtils.isNotEmpty(shipid)){
            model.addAttribute("shipid",shipid);
        }
        return "/modules/map/window/control";
    }
    @RequestMapping("/ship/serverPoint")
    public String shipServerPoint(Model model){
        return "/modules/map/window/serverPoint";
    }

    @RequestMapping("/ship/serverShip")
    public String serverShip(Model model){
        return "/modules/map/window/serverShip";
    }

    @RequestMapping("/warning")
    public String companyWarning(Model model){
        return "/modules/map/window/companyWarning";
    }

}
